<?php

namespace app\api\controller\Project;

use think\Controller;

class Order extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->OrderModel = model('Project.Order');

        $this->UserModel = model('User.User');

        $this->RecordModel = model('User.Record');
    }
    
    public function index()
    {
        $userid = $this->request->param('userid');

        $status = $this->request->param('status');

        $user = model('User.User')->find($userid);

        if(!$user)
        {
            $this->error('用户不存在，请先绑定账号');
        }

        $where = ['userid' => $userid];

        if(!empty($status) || $status === (String)0)
        {
            
            $where['status'] = $status;
        }

        $OrderList = $this->OrderModel->with(['project'])->where($where)->order('id','desc')->select();

        $this->success('查询成功',null,$OrderList);
    }
	
	// 撤销预约
	public function cancel()
	{
		$userid = $this->request->param('userid');
		
		$orderid = $this->request->param('orderid');
		
		$user = model('User.User')->find($userid);
		
		if(!$user)
		{
		    $this->error('用户不存在，请先绑定账号');
		}
		
		$order = $this->OrderModel->find($orderid);
		
		if(!$order)
		{
		    $this->error('该订单不存在，请重新选择');
		}
		
		$data = [
			'id' => $orderid,
			'status' => $order['previousstatus'],
			'previousstatus' => $order['previousstatus']
		];
		
		$result = $this->OrderModel->isUpdate(true)->save($data);
		
		if($result === FALSE)
		{
		    $this->error('撤销预约失败');
		}else{
		    $this->success('撤销预约成功');
		}
	}

    public function add()
    {
        $userid = $this->request->param('userid');

        $user = model('User.User')->find($userid);

        $projectid = $this->request->param('projectid');

        $project = model('Project.Project')->find($projectid);

        $addrid = $this->request->param('addrid');

        $address = model('User.Address')->find($addrid);

        if(!$user)
        {
            $this->error('用户不存在。请先绑定账号');
        }

        if(!$project)
        {
            $this->error('选择的预约服务不存在，请重新选择');
        }

        if(!$address)
        {
            $this->error('选择的上门地址不存在，请重新选择');
        }

        $params = $this->request->param();

        $data = [
            'userid' => $userid,
            'addrid' => $addrid,
            'projectid' => $projectid,
            'content' => $params['content'],
            'createtime' => $params['createtime'],
            'status' => 0,
			'previousstatus' => 0,
        ];

        $result = $this->OrderModel->validate('common/Project/Order')->save($data);

        if($result === FALSE)
        {
            $this->error($this->OrderModel->getError());
        }else{
            $this->success('预约成功','/pages/order/index');
        }
    }

    public function pay()
    {
        $userid = $this->request->param('userid');

        $orderid = $this->request->param('orderid');

        $user = model('User.User')->find($userid);

        if(!$user)
        {
            $this->error('用户不存在，请先绑定账号');
        }

        $order = $this->OrderModel->with(['project'])->find($orderid);

        if(!$order)
        {
            $this->error('该订单不存在，请重新选择');
        }

        // ask论坛的积分来抵扣金额 没接口
        if($user['point'] < $order['price'])
        {
            $this->error('您的余额不足');
        }

        /* 
            用户表 更新积分
            消费记录 新增记录
            预约订单 更新状态
        */

        // 开启事务
        $this->UserModel->startTrans();
        $this->RecordModel->startTrans();
        $this->OrderModel->startTrans();

        // 用户更新数据
        $UserData = [
            'id' => $userid,
            'point' => bcsub($user['point'],$order['price'])
        ];

        $UserStatus = $this->UserModel->isUpdate(true)->save($UserData);

        if($UserStatus === FALSE)
        {
            $this->error($this->UserModel->getError());
        }

        // 新增消费记录
        $RecordData = [
            'point' => $order['price'],
            'content' => "您的预约服务{$order['project']['name']}消费了{$order['price']}积分",
            'state' => 6,
            'userid' => $userid
        ];

        $RecordStatus = $this->RecordModel->validate('common/User/Record')->save($RecordData);

        if($RecordStatus === FALSE)
        {
            $this->UserModel->rollback();
            $this->error($this->RecordModel->getError());
        }

        // 预约状态
        $OrderData = [
            'id' => $orderid,
            'status' => 2,
			'previousstatus' => 2
        ];

        $OrderStatus = $this->OrderModel->isUpdate(true)->save($OrderData);

        if($OrderStatus === FALSE)
        {
            $this->UserModel->rollback();
            $this->RecordModel->rollback();
            $this->error($this->OrderModel->getError());
        }

        if($UserStatus === FALSE || $RecordStatus === FALSE || $OrderStatus === FALSE)
        {
            $this->UserModel->rollback();
            $this->RecordModel->rollback();
            $this->OrderModel->rollback();
            $this->error('支付失败');
        }else{
            $this->UserModel->commit();
            $this->RecordModel->commit();
            $this->OrderModel->commit();
            $this->success('支付成功');
        }
    }

    public function revoke()
    {
        $userid = $this->request->param('userid');

        $orderid = $this->request->param('orderid');

        $user = model('User.User')->find($userid);

        if(!$user)
        {
            $this->error('用户不存在，请先绑定账号');
        }

        $order = $this->OrderModel->with(['project'])->find($orderid);

        if(!$order)
        {
            $this->error('该订单不存在，请重新选择');
        }
        
        $OrderData = [
            'id' => $orderid,
            'status' => -2
        ];

        $result = $this->OrderModel->isUpdate(true)->save($OrderData);

        if($result === FALSE)
        {
            $this->error('申请撤销失败');
        }else{
            $this->success('等待审核');
        }
    }

    public function info()
    {
        $orderid = $this->request->param('orderid');

        $order = $this->OrderModel->with(['user','project','address'])->find($orderid);

        if(!$order)
        {
            $this->error('该预约不存在，请重新选择');
        }

        $this->success('查询成功',null,$order);
    }
}
